
package com.shangsi.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * https://www.cnblogs.com/ldmy/p/14302332.html
 * 整合nacos，持久化限流规则
 * @author dzd
 * @date 2021-12-15
 */
@RestController
public class SentinelFlowController {

    /**
     * 注解的方式，原理为aop
     * @return Str
     */
    @SentinelResource(value = "HelloWorld_SpringCloud",blockHandler = "blockHandlerForHelloWorld3")
    @RequestMapping("HelloWorldSpringCloud")
    public String helloWorld3() {
        return "sentinel 大家好 HelloWorldSpringCloud！" + System.currentTimeMillis();
    }

    /**
     * 如果直接对这个请求进行限流配置 请求路径可以作为一个资源名
     * 默认的限流返回结果Blocked by Sentinel (flow limiting)
     */
    @RequestMapping("defaultLimitFlow")
    public String defaultLimitFlow() throws InterruptedException {
        //TimeUnit.SECONDS.sleep(1);
        System.out.println(Thread.currentThread().getName()+":"+System.currentTimeMillis());
        return "sentinel 大家好 defaultLimitFlow！" + System.currentTimeMillis();
    }


    /**
     * 注解限流的时候的降级方法
     * @param blockException blockException
     * @return str
     */
    public String blockHandlerForHelloWorld3(BlockException blockException){
        blockException.printStackTrace();
        return "系统繁忙，请稍后 HelloWorldSpringCloud!";
    }

}
